package com.linewow.xhyy.nihaonew.adapter;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.linewow.xhyy.nihaonew.R;
import com.linewow.xhyy.nihaonew.entity.NewTopicImageInfo;
import com.linewow.xhyy.nihaonew.helper.ItemDragHelperCallback;
import com.linewow.xhyy.nihaonew.helper.OnItemMoveListener;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.List;


/**
 * Created by LXR on 2016/9/22.
 */
public class NewTopicAdapter extends BaseQuickAdapter<NewTopicImageInfo> implements OnItemMoveListener {
    private final ItemTouchHelper helper;
    private Context context;
    private List<NewTopicImageInfo> list;
    private boolean editFlag;

    public NewTopicAdapter(List<NewTopicImageInfo> data) {
        super(R.layout.item_new_topic,data);
        list=data;
        ItemDragHelperCallback callback = new ItemDragHelperCallback(){
            @Override
            public boolean isLongPressDragEnabled() {
                // 长按拖拽打开
                return true;
            }
        };
        helper = new ItemTouchHelper(callback);
    }

    public ItemTouchHelper getHelper() {
        return helper;
    }

    @Override
    protected void convert(final BaseViewHolder baseViewHolder, NewTopicImageInfo newTopicImageInfo) {

        if(context==null){
            context=baseViewHolder.getConvertView().getContext();
        }
        if(editFlag){
            baseViewHolder.setVisible(R.id.item_new_topic_del,true);
            baseViewHolder.setVisible(R.id.item_new_topic_move,true);
        }else{
            baseViewHolder.setVisible(R.id.item_new_topic_del,false);
            baseViewHolder.setVisible(R.id.item_new_topic_move,false);
        }
        ImageView imageView=baseViewHolder.getView(R.id.item_new_topic_image);
        Picasso.with(context).load(new File(newTopicImageInfo.path)).fit().centerCrop().error(R.mipmap.bg_error).into(imageView);
        baseViewHolder.setText(R.id.item_new_topic_edit,newTopicImageInfo.description);
        baseViewHolder.addOnClickListener(R.id.item_new_topic_image);
        baseViewHolder.addOnClickListener(R.id.item_new_topic_del);
        ImageView topicMove=baseViewHolder.getView(R.id.item_new_topic_move);


        Log.e("NewTopicAdapter","设置监听了");
        topicMove.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(MotionEventCompat.getActionMasked(motionEvent)==MotionEvent.ACTION_DOWN){
                    helper.startDrag(baseViewHolder);
                }
                return false;
            }
        });
    }


    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        NewTopicImageInfo info=list.get(fromPosition);
        list.remove(fromPosition);
        list.add(toPosition,info);
        notifyItemMoved(fromPosition,toPosition);
    }

    public void setEditFlag(boolean editFlag) {
        this.editFlag = editFlag;
        notifyDataSetChanged();
    }


}
